package com.gzy.rpsm.property.mapper;

import com.github.pagehelper.Page;
import com.gzy.rpsm.property.pojo.Pay;
import com.gzy.rpsm.property.pojo.PayInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;

@Repository
public interface PayMapper {
    @Select("<script>select payInfo.pay_id,payInfo.building_id,payInfo.room_id,u.userid from (select p.pay_id,b.building_id,r.room_id,f.floor_id from (select pay.pay_id,pay.pay_room_id from pay " +
            "<where> <if test='start != null and end != null' >pay.pay_date BETWEEN #{start} and #{end} </if></where>) p,building b,(select r.room_id,r.room_floor_id from room r " +
            "<where> <if test='floorId != null'>r.room_floor_id = #{floorId} </if></where> union select room.room_id,room.room_floor_id from room,floor WHERE " +
            "<if test='buildingId != null and floorId == null'>floor.belong_building = #{buildingId} and </if>room.room_floor_id = floor.floor_id ) r,floor f " +
            "where r.room_floor_id = f.floor_id and f.belong_building = b.building_id " + "and p.pay_room_id = r.room_id) payInfo LEFT JOIN (select userid,user_roomid from `user` " +
            "<where> <if test='username != null and username != \"\"'>u.username like concat('%',#{username},'%') </if><if test='idCard != null and idCard != \"\"'>and u.idcard like concat('%',#{idCard},'%') </if></where>" +
            ") u on u.user_roomid = payInfo.floor_id</script>")
    //Localdate不能与""进行对比
    @Results(id="payInfo",value = {
            @Result(property = "user",column = "userid",one =@One(select = "com.gzy.rpsm.property.mapper.UserMapper.queryById")),
            @Result(property = "building",column = "building_id",one = @One(select="com.gzy.rpsm.property.mapper.BuildingMapper.queryById")),
            @Result(property = "room",column = "room_id",one = @One(select="com.gzy.rpsm.property.mapper.RoomMapper.roomById")),
            @Result(property = "pay",column = "pay_id",one = @One(select="com.gzy.rpsm.property.mapper.PayMapper.queryById")),
    })
    Page<PayInfo> selectPayInfoOnCondition(
            @Param("start") LocalDate start,
            @Param("end") LocalDate end,
            @Param("username") String username,
            @Param("idCard") String idCard,
            @Param("floorId") Integer floorId,
            @Param("buildingId") Integer buildingId);

    @Select("<script>" +
            "select * from pay where pay_id = #{id}" +
            "</script>")
    Pay queryById(Integer id);
}